﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;

/// <summary>
/// Summary description for pdfPage
/// </summary>
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
	public pdfPage()
	{
		
	}

    protected Font footer
    {
        get
        {
            // create a basecolor to use for the footer font, if needed.
            BaseColor grey = new BaseColor(128, 128, 128);
            string fontpath =HttpContext.Current.Server.MapPath("/Admin/PdfFonts/");
            BaseFont customfont = BaseFont.CreateFont(fontpath + "TIMES.TTF", BaseFont.IDENTITY_H, true);
            Font font = new Font(customfont, 14, Font.NORMAL);
            return font;
        }
    }

    public override void OnEndPage(PdfWriter writer, Document doc)
    {
        //I use a PdfPtable with 2 columns to position my footer where I want it
        PdfPTable footerTbl = new PdfPTable(2);
        //set the width of the table to be the same as the document
        footerTbl.TotalWidth = doc.PageSize.Width;
        //Center the table on the page
        footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
        //create new instance of Paragraph for 2nd cell text
        Paragraph para = new Paragraph("Trang " + doc.PageNumber, footer);
        //create new instance of cell to hold the text
        PdfPCell cell = new PdfPCell(para);
        //align the text to the right of the cell
        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
        //set border to 0
        cell.Border = 0;
        cell.Colspan = 2;

        // add some padding to take away from the edge of the page
        cell.PaddingRight = 10;

        //add the cell to the table
        footerTbl.AddCell(cell);
        //write the rows out to the PDF output stream.
        footerTbl.WriteSelectedRows(0, -1, 0, (doc.BottomMargin + 10), writer.DirectContent);
    }
   
}